<?php

/**
 * Base actions for the jfPortableDevicePlugin jfPortableDevice module.
 * 
 * @package     jfPortableDevicePlugin
 * @subpackage  jfPortableDevice
 * @author      Jonas Fischer <info@jonasfischer.net>
 * @version     SVN: $Id: BaseActions.class.php 12534 2008-11-01 13:38:27Z Kris.Wallsmith $
 */
abstract class BasejfPortableDeviceActions extends sfActions
{

	public function executeRedirect()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
  	jfPortableDevice::setIsPortableDevice(true);
  	jfPortableDevice::setAppType('web');
  	jfPortableDevice::setDeviceSize($this->getDeviceSize());

  	$redirectUrl = $request->getParameter('redirectUrl', $this->getController()->genUrl(jfPortableDevice::getConfig('startUrl'), true));
  	$redirectUrl = jfPortableDevice::extendUrl($redirectUrl);

  	$this->redirect($redirectUrl, 302);
    throw new sfStopException();
  }
  
  protected function getWurflDevice() {
		$wurflConfigFile = jfPortableDevice::getConfig('wurflConfigFile');
		
		if (!$wurflConfigFile) {
			return null;
		}
		
		$wurflDir = dirname(__FILE__) . "/../../../lib/vendor/WURFL_PHP/WURFL/";
	
		
		require_once $wurflDir.'ClassLoader.php';
		
		// Create WURFL Configuration from an XML config file
		$wurflConfig = new WURFL_Configuration_XmlConfig($wurflConfigFile);
		// Create a WURFL Manager Factory from the WURFL Configuration
		$wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);
		// Create a WURFL Manager ($wurflManager is a WURFL_WURFLManager object)
		$wurflManager = $wurflManagerFactory->create();

		// Get information on the loaded WURFL ($wurflInfo is a WURFL_Xml_Info object)
		//$wurflInfo = $wurflManager->getWURFLInfo();
		
		return $wurflManager->getDeviceForHttpRequest($_SERVER);
  }
  
  protected function getDeviceSize() {
	 	try {
  		$wurflDevice = $this->getWurflDevice();
  		if ($wurflDevice) {
	  		$screenWidth = $wurflDevice->getCapability("resolution_width");
	  		$screenHeight = $wurflDevice->getCapability("resolution_height");
	
	  		if ($screenWidth <= 250 || $screenHeight <= 250) {
	  			return jfPortableDevice::DEVICE_SIZE_S;
	  		}
	  		else if ($screenWidth > 800 || $screenHeight > 800) {
	  			return jfPortableDevice::DEVICE_SIZE_L;
	  		}
	  		else {
	  			return jfPortableDevice::DEVICE_SIZE_M;
	  		}
  		}
  	}
  	catch (Exception $e) {}
  	
  	return jfPortableDevice::DEVICE_SIZE_M;
  }
	
  public function executeAbout()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
  	jfPortableDevice::setConfig('useLogo', false);
  	jfPortableDevice::setConfig('useTabBar', false);
  }

  public function executeManifest()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
    $context = $this->getContext();
    if(method_exists($context, 'getConfiguration') && method_exists($context->getConfiguration(), 'loadHelpers')) {
		$context->getConfiguration()->loadHelpers('Url');
    }
    else if (method_exists('sfLoader', 'loadHelpers')) {
      sfLoader::loadHelpers('Url'); //compatibility with symfony 1.0:
    }

  	sfConfig::set('sf_web_debug', false);
    $this->setLayout(false);
		$this->getResponse()->setContentType('text/cache-manifest');

		$this->files = array();
		
		if (!jfPortableDevice::isNativeApp()) {
			//$this->files[] = url_for('@jfPortableDevice_redirect');
			$this->files[] = jfPortableDevice::extendUrl(url_for(jfPortableDevice::getConfig('startUrl')));
			$this->files = $this->getCacheManifestFilesForConfig(jfPortableDevice::getConfig('includeJavascriptsForApp'), 'javascript_path', $this->files);
			$this->files = $this->getCacheManifestFilesForConfig(jfPortableDevice::getConfig('includeStylesheetsForApp'), 'stylesheet_path', $this->files);
			$this->files = $this->getCacheManifestFilesForDirectory(sfConfig::get('sf_web_dir').jfPortableDevice::getConfig('jfPDTheme'), $this->files);
		}

  }
  
  protected function getCacheManifestFilesForConfig($config, $callback, $files = array()) {
  	
    $context = $this->getContext();
  	// compatibility with symfony 1.0:
  	if(method_exists($context, 'getConfiguration') && method_exists($context->getConfiguration(), 'loadHelpers')) {
      $context->getConfiguration()->loadHelpers('Asset');
    }
    else if (method_exists('sfLoader', 'loadHelpers')) {
      sfLoader::loadHelpers('Asset'); //maintain compatibility to symfony versions prior to 1.3
    }
  	
  	foreach ($config as $key => $value) {
  		if (is_string($value)) {
  			$files[] = $callback($value);
  		}
  		else if (is_array($value)) {
  			foreach ($value as $filename => $options) {
  				$files[] = $callback($filename);
  			}
  		}
  	}

  	return $files;
  }
  
  protected function getCacheManifestFilesForDirectory($dir, $files = array()) {
  	$dirIterator = new RecursiveDirectoryIterator($dir);
		foreach(new RecursiveIteratorIterator($dirIterator) as $file) {
		  if ($file->IsFile() &&
		      substr($file->getFilename(), 0, 1) != "." &&
		      false === strpos((string)$file, '.svn')
		  ) {
		    $files[] = $file;
		  }
		}
		return $files;
  }

  public function executeOffline()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
    $this->setLayout(false);
  }

}
